package com.chaosj.tools

import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardOpenOption
import java.util.concurrent.ConcurrentLinkedQueue

/**
 * @className: AllMarkDownProcess* @description:
 * @author: caoyangjie
 * @date: 2022/1/4
 * */
class AllMarkDownProcess {
    static void main(args){
        String projectPath = "/home/caoyangjie/IdeaProjects/spring-stack-skills"
        String writePath = "/home/caoyangjie/logs/allMarkdown.md"
        scannerMd(projectPath, writePath)
    }

    static void scannerMd(projectPath, writePath){
        ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>()
        getFileList(projectPath, new ArrayList<File>())
            .forEach({file->
                Files.readAllLines(file.toPath()).forEach({
                    line ->
                        if( careLine(line) ){
                            queue.add(line)
                        }
                })
            })
        queue.each {line->
            Files.write(Paths.get(writePath), (line+System.lineSeparator()).getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND)
        }
    }

    static boolean careLine(String line){
        line.contains("[")&&line.contains("]")&&line.contains("(http")&&line.contains(")")
    }

    static List<File> getFileList(String strPath,List<File> filelist) {
        File dir = new File(strPath);
        File[] files = dir.listFiles();
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                String fileName = files[i].getName();
                if (files[i].isDirectory()) {
                    getFileList(files[i].getAbsolutePath(),filelist);
                } else {
                    if( fileName.endsWith(".md") ){
                        filelist.add(files[i]);
                    }
                }
            }

        }
        return filelist;
    }
}
